/**
 * flutter 文件接口
 * 执行失败会抛出异常，调用接口时需要考虑异常，用 try / catch 包裹
 * /

/**
 * 查询根目录
 * @returns 返回完整路径
 */
const getRootPath = async () => {
  const data = await window.flutter_inappwebview.callHandler(
    "getRootPath",
    "applicationDocuments"
  );
  // {"path" : "/data/user/0/com.summer.flutter_app/app_flutter"}
  return data.path;
};

/**
 * 查询根目录
 * @param fullPathName
 * @returns 返回完整路径
 */
const fileListInDirectory = async (fullPathName: string) => {
  const data = await window.flutter_inappwebview.callHandler(
    "fileListInDirectory",
    fullPathName,
    // recursive
    false,
    // followLinks
    true
  );
  // 返回数据结构
  //[
  //{"path":"/data/user/0/com.summer.flutter_app/app_flutter/flutter_assets", "type":"directory"},
  //{"path":"/data/user/0/com.summer.flutter_app/app_flutter/res_timestamp-1-1684140496669", "type":"file"}
  //]
  return data;
};

/**
 * 新建文件夹
 * @param fullPathName 每次只能创建一级目录，需要传入包含要创建文件夹名称的全路径
 * @returns 返回完整路径
 */
const newDirectory = async (fullPathName: string) => {
  const data = await window.flutter_inappwebview.callHandler(
    "newDirectory",
    fullPathName
  );
  // {"path": directory.path};
  return data.path;
};

/**
 * 判断文件夹是否存在
 * @param fullPathName 每次只能创建一级目录，需要传入包含要创建文件夹名称的全路径
 * @returns 返回完整路径
 */
const existsDirectory = async (fullPathName: string) => {
  const data = await window.flutter_inappwebview.callHandler(
    "existsDirectory",
    fullPathName
  );
  // {"existsDirectory": existsDirectory,};
  // 返回 true\false
  return data.existsDirectory;
};

/**
 * 删除文件夹
 * @param fullPathName 每次只能删除一级目录，需要传入全路径，如果文件夹内有文件，则删除失败
 */
const deleteDirectory = async (fullPathName: string) => {
  const data = await window.flutter_inappwebview.callHandler(
    "deleteDirectory",
    fullPathName
  );
  // {"path": fileSystemEntity.path};
  return data.path;
};

/**
 * 写入文件
 * @param fullFileName 全路径文件名
 * @param base64string 文件必须是 base64 编码
 * @returns 全路径文件名
 */
const writeFile = async (fullFileName: string, base64string: string) => {
  const data = await window.flutter_inappwebview.callHandler(
    "writeFile",
    fullFileName,
    base64string
  );
  //{"path": file.path};
  return data.path;
};

/**
 * 读取文件
 * @param fullFileName 全路径文件名
 * @returns base64 编码后的文件内容
 */
const readFile = async (fullFileName: string) => {
  const data = await window.flutter_inappwebview.callHandler(
    "readFile",
    fullFileName
  );
  //{"fileBase64": fileBase64}
  return data.fileBase64;
};

/**
 * 判断文件是否存在
 * @param fullFileName 全路径文件名
 * @returns
 */
const existsFile = async (fullFileName: string) => {
  const data = await window.flutter_inappwebview.callHandler(
    "existsFile",
    fullFileName
  );
  // "existsFile": existsFile,
  // 返回 true\false
  return data.existsFile;
};

/**
 * 删除文件
 * @param fullFileName 全路径文件名
 * @returns 返回完整路径
 */
const deleteFile = async (fullFileName: string) => {
  const data = await window.flutter_inappwebview.callHandler(
    "deleteFile",
    fullFileName,
    //recursive
    false
  );
  //{"path": file.path};
  return data.path;
};

export default {
  getRootPath,
  fileListInDirectory,
  newDirectory,
  existsDirectory,
  deleteDirectory,
  writeFile,
  readFile,
  existsFile,
  deleteFile,
};
